package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.baihang.BaihangPortraitFields;
import com.ucredit.babel.common.model.third.baihang.BaihangPortraitRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

/**
 * 百行-反欺诈风险画像
 *
 * @author guoqiang
 */
public class BaihangPortraitParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 百行-反欺诈风险画像
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BaihangPortraitRecord obj = (BaihangPortraitRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(formatValue(obj.getCode())).append(CRT001);
        sb.append(formatValue(obj.getMessage())).append(CRT001);
        sb.append(formatValue(obj.getErrorCode())).append(CRT001);
        sb.append(formatValue(obj.getErrorMessage())).append(CRT001);
        sb.append(formatValue(obj.getBatNo())).append(CRT001);
        sb.append(formatValue(obj.getReceiveTime())).append(CRT001);
        sb.append(formatValue(obj.getHasSystemError())).append(CRT001);
        sb.append(formatValue(obj.getResult())).append(CRT001);
        sb.append(formatValue(obj.getRiskView())).append(CRT001);
        sb.append(formatValue(obj.getViewValue())).append(CRT001);

        List<BaihangPortraitFields> fields = obj.getFields();
        StringBuilder TxSb = new StringBuilder(500);
        for(BaihangPortraitFields filed: fields) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(formatValue(filed.getId())).append(CRT001);
            TxSb.append(formatValue(filed.getName())).append(CRT001);
            TxSb.append(formatValue(filed.getValue())).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_baihang_portrait_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIHANG_PORTRAIT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "百行-反欺诈风险画像";
    }
}
